package com.ymatou.iapi.dguser.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.member.parameter.CreateMemberBean;
import com.ymatou.iapi.dguser.parameter.GetUserBindInfoByAppnameBean;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.dguser.service.GetUserBindInfoByAppnameCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.DGThirdPartyBindInfo;
/**
*下获取用户某个微信小程序的绑定信息
*FreeMaker Template自动生成代码
*/
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.utils.TestDataManager;
@TestSuite
public class Ts_GetUserBindInfoByAppname{
	private static GetUserBindInfoByAppnameBean getuserbindinfobyappnameBean;
	private static GetUserBindInfoByAppnameCall getuserbindinfobyappnameCall;
	DGUserWapper DGUserWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("下获取用户某个微信小程序的绑定信息");
	}

	@Before
	public void caseUp() {
		getuserbindinfobyappnameBean=new GetUserBindInfoByAppnameBean();
		getuserbindinfobyappnameCall=new GetUserBindInfoByAppnameCall();
		DGUserWapper=new DGUserWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getuserbindinfobyappnameCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(String openId) {
		DGThirdPartyBindInfo dgThirdPartyBindInfo = DGUserWapper.selectDGThirdPartyOpenId(openId).get(0);
		JsonObject jet = getuserbindinfobyappnameCall.getJsonObject().get("userBindDto").getAsJsonObject();
		Logger.verifyEquals(jet.get("bindId").getAsInt(),dgThirdPartyBindInfo.getBindId(),"验证bindId");
		Logger.verifyEquals(jet.get("userType").getAsInt(),dgThirdPartyBindInfo.getUserType(),"验证userType");
		Logger.verifyEquals(jet.get("thirdPartyType").getAsInt(),dgThirdPartyBindInfo.getThirdPartyType(),"验证thirdPartyType");
		Logger.verifyEquals(jet.get("appSource").getAsInt(),dgThirdPartyBindInfo.getAppSource(),"验证appSource");
		Logger.verifyEquals(jet.get("unionId").getAsString(),dgThirdPartyBindInfo.getUnionId(),"验证unionId");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserBindInfoByAppname_001() {
		Logger.start(true,"获取用户某个微信小程序的绑定信息,成功-默认用户");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
//			usercreateBean.setAppSource(DGUserCallService.DG_SellerWxApp);
//			usercreateBean.setWxAppName(DGUserCallService.TestWxsqOther);
//			usercreateBean.setBelongSellerId(DGUserCallService.TestWxsqOtherSellerId);
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			getuserbindinfobyappnameBean.setUserId(userId);
			getuserbindinfobyappnameBean.setWxAppName(usercreateBean.getWxAppName());
			getuserbindinfobyappnameCall.setData(getuserbindinfobyappnameBean);
			getuserbindinfobyappnameCall.callService();			
			Logger.verifyEquals("true", getuserbindinfobyappnameCall.getString("success"), "验证success");
			verifyDB(usercreateBean.getOpenId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserBindInfoByAppname_002() {
		Logger.start(true,"获取用户某个微信小程序的绑定信息,成功-默认商家");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initSellerUsercreateBean();
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			getuserbindinfobyappnameBean.setUserId(userId);
			getuserbindinfobyappnameBean.setWxAppName(usercreateBean.getWxAppName());
			getuserbindinfobyappnameCall.setData(getuserbindinfobyappnameBean);
			getuserbindinfobyappnameCall.callService();			
			Logger.verifyEquals("true", getuserbindinfobyappnameCall.getString("success"), "验证success");
			verifyDB(usercreateBean.getOpenId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserBindInfoByAppname_003() {
		Logger.start(true,"获取用户某个微信小程序的绑定信息,成功-默认客服");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			getuserbindinfobyappnameBean.setUserId(userId);
			getuserbindinfobyappnameBean.setWxAppName(usercreateBean.getWxAppName());
			getuserbindinfobyappnameCall.setData(getuserbindinfobyappnameBean);
			getuserbindinfobyappnameCall.callService();			
			Logger.verifyEquals("true", getuserbindinfobyappnameCall.getString("success"), "验证success");
			verifyDB(usercreateBean.getOpenId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserBindInfoByAppname_004() {
		Logger.start(true,"获取用户某个微信小程序的绑定信息,成功-独立用户");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			usercreateBean.setAppSource(DGUserCallService.DG_SellerWxApp);
			usercreateBean.setWxAppName(DGUserCallService.TestWxsqOther);
			usercreateBean.setBelongSellerId(DGUserCallService.TestWxsqOtherSellerId);
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			getuserbindinfobyappnameBean.setUserId(userId);
			getuserbindinfobyappnameBean.setWxAppName(usercreateBean.getWxAppName());
			getuserbindinfobyappnameCall.setData(getuserbindinfobyappnameBean);
			getuserbindinfobyappnameCall.callService();			
			Logger.verifyEquals("true", getuserbindinfobyappnameCall.getString("success"), "验证success");
			verifyDB(usercreateBean.getOpenId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserBindInfoByAppname_005() {
		Logger.start(true,"不输入uid失败");
		try {
//			getuserbindinfobyappnameBean.setUserId(userId);
			getuserbindinfobyappnameBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			getuserbindinfobyappnameCall.setData(getuserbindinfobyappnameBean);
			getuserbindinfobyappnameCall.callService();			
			Logger.verifyEquals("false", getuserbindinfobyappnameCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserBindInfoByAppname_006() {
		Logger.start(true,"不输入wxAppName失败");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			getuserbindinfobyappnameBean.setUserId(userId);
//			getuserbindinfobyappnameBean.setWxAppName(usercreateBean.getWxAppName());
			getuserbindinfobyappnameCall.setData(getuserbindinfobyappnameBean);
			getuserbindinfobyappnameCall.callService();			
			Logger.verifyEquals("false", getuserbindinfobyappnameCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserBindInfoByAppname_007() {
		Logger.start(true,"不存在uid失败");
		try {
			getuserbindinfobyappnameBean.setUserId(100);
			getuserbindinfobyappnameBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			getuserbindinfobyappnameCall.setData(getuserbindinfobyappnameBean);
			getuserbindinfobyappnameCall.callService();			
			Logger.verifyEquals("true", getuserbindinfobyappnameCall.getString("success"), "验证success");
			Logger.verifyEquals(false, getuserbindinfobyappnameCall.getJsonObject().has("userBindDto"), "验证空数据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserBindInfoByAppname_008() {
		Logger.start(true,"uid和wxAppname不匹配失败");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			usercreateBean.setAppSource(DGUserCallService.DG_SellerWxApp);
			usercreateBean.setWxAppName(DGUserCallService.TestWxsqOther);
			usercreateBean.setBelongSellerId(DGUserCallService.TestWxsqOtherSellerId);
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			getuserbindinfobyappnameBean.setUserId(userId);
			getuserbindinfobyappnameBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			getuserbindinfobyappnameCall.setData(getuserbindinfobyappnameBean);
			getuserbindinfobyappnameCall.callService();			
			Logger.verifyEquals("true", getuserbindinfobyappnameCall.getString("success"), "验证success");
			Logger.verifyEquals(false, getuserbindinfobyappnameCall.getJsonObject().has("userBindDto"), "验证空数据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserBindInfoByAppname_009() {
		Logger.start(true,"获取用户某个微信小程序的绑定信息,成功-2个userid同一个手机号绑定一个shopid场景");
		//从会员系统查询
		try {
			//2个userid 创建了2个会员 一个h5 一个小程序 使用同一个手机号绑定 在支付查询的时候需要按场景(h5 小程序)返回对应的openid
			Integer shopid = GodmchManageCallService.CreateSellerMch();
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			int userId=DGUserCallService.UserCreateCall(usercreateBean);
			UserCreateBean usercreateBean2= DGUserCallService.initUsercreateBean();
			int userId2=DGUserCallService.UserCreateCall(usercreateBean2);
			String mobile=TestDataManager.getPhoneNum();
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			//60.小程序 61.h5
			createMem.setAccessSource(0);
			GodMemberService.creatememberCall(createMem);
			
			CreateMemberBean createMem2 = GodMemberService.initCreateMemberBean(shopid,userId2,usercreateBean2.getOpenId());
			//60.小程序 61.h5
			createMem2.setAccessSource(1);
			GodMemberService.creatememberCall(createMem2);
			
			GodMemberService.bindMobile(shopid, userId, mobile);
			GodMemberService.bindMobile(shopid, userId2, mobile);
			
			getuserbindinfobyappnameBean.setUserId(userId);
			getuserbindinfobyappnameBean.setShopId(shopid);
			getuserbindinfobyappnameBean.setAppSource(60);
			getuserbindinfobyappnameBean.setPayScene(true);
			getuserbindinfobyappnameBean.setWxAppName(usercreateBean.getWxAppName());
			getuserbindinfobyappnameCall.setData(getuserbindinfobyappnameBean);
			getuserbindinfobyappnameCall.callService();			
			Logger.verifyEquals("true", getuserbindinfobyappnameCall.getString("success"), "验证success");
			verifyDB(usercreateBean.getOpenId());
			
			getuserbindinfobyappnameBean.setAppSource(61);
			getuserbindinfobyappnameCall.callService();			
			Logger.verifyEquals("true", getuserbindinfobyappnameCall.getString("success"), "验证success");
			verifyDB(usercreateBean2.getOpenId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}